#include "psglobject.h"
#include "psgl_draw_utils.h"

PSGLObject::PSGLObject()
{
  xRot = 0.0;
  yRot = 0.0;
  zRot = 0.0;
}

PSGLObject::PSGLObject(QString p_name, QVector<Point3D> p_trajectory, GLfloat p_nSca, GLfloat p_cr, GLfloat p_cg, GLfloat p_cb, GLfloat p_ca)
{
  xRot = 0.0;
  yRot = 0.0;
  zRot = 0.0;
  trajectory = p_trajectory;
  name = p_name;
  nSca = p_nSca;
  cr = p_cr;
  cg = p_cg;
  cb = p_cb;
  ca = p_ca;
}

void PSGLObject::render()  //testing purposes
{
    glPushMatrix();
    glTranslatef(xTra, yTra, zTra);
    glScalef(nSca, nSca, nSca);
    glRotatef(xRot, 1.0f, 0.0f, 0.0f);
    glRotatef(yRot, 0.0f, 1.0f, 0.0f);
    glRotatef(zRot, 0.0f, 0.0f, 1.0f);

    drawSphere(cr, cg, cb, ca);
    glPopMatrix();

}

void PSGLObject::render(GLuint frameNumber)
{
    int n = frameNumber;
    if(trajectory.size() > n)
    {

      glPushMatrix();
      glTranslated(trajectory[n].x, trajectory[n].y, trajectory[n].z);
      glScalef(nSca, nSca, nSca);
      glRotatef(xRot, 1.0f, 0.0f, 0.0f);
      glRotatef(yRot, 0.0f, 1.0f, 0.0f);
      glRotatef(zRot, 0.0f, 0.0f, 1.0f);

      drawSphere(cr, cg, cb, ca);
      glPopMatrix();
    }
}

void PSGLObject::renderText()
{
    glColor4f(cr, cg, cb, ca);
    glPushMatrix();
    glTranslatef(xTra, yTra, zTra);
    glRotatef(90.0, 1.0f, 0.0f, 0.0f);
    //glScalef(nSca / 2.0f, nSca / 2.0f, nSca / 2.0f);
    glTranslatef(nSca, 0.0, 0.0);
    QPainterPath path;
    QFont font("Arial", 2);
    path.addText(QPointF(0, 0), QFont("Arial", 2), name);
    QList<QPolygonF> poly = path.toSubpathPolygons();
    for (QList<QPolygonF>::iterator i = poly.begin(); i != poly.end(); i++){
        glBegin(GL_LINE_LOOP);
        for (QPolygonF::iterator p = (*i).begin(); p != i->end(); p++)
            glVertex3f(p->rx()*0.1f, -p->ry()*0.1f, 0);
        glEnd();
    }

    glPopMatrix();
}


void PSGLObject::renderText(GLuint frameNumber)
{
    glColor4f(cr, cg, cb, ca);
    int n = frameNumber;
    if(trajectory.size() > n)
    {
      glPushMatrix();
      glTranslated(trajectory[n].x + (0.7)*nSca, trajectory[n].y, trajectory[n].z + (0.7)*nSca);
      glRotatef(90.0, 1.0f, 0.0f, 0.0f);
      //glScalef(nSca, nSca, nSca);
      QPainterPath path;
      QFont font("Arial", 2);
      path.addText(QPointF(0, 0), QFont("Arial", 2), name);
      QList<QPolygonF> poly = path.toSubpathPolygons();
      for (QList<QPolygonF>::iterator i = poly.begin(); i != poly.end(); i++){
         glBegin(GL_LINE_LOOP);
          for (QPolygonF::iterator p = (*i).begin(); p != i->end(); p++)
              glVertex3f(p->rx()*0.1f, -p->ry()*0.1f, 0);
         glEnd();
    }
    }

    glPopMatrix();
}

void PSGLObject::renderTrajectory()
{
  int step = trajectory.size() / 10000;
  if (step == 0)
    step = 1;
  if (!trajectory.empty())
  {
    glBegin(GL_LINES);
    glColor4f(cr, cg, cb, ca);
    for (int i = step; i < trajectory.size(); i += step)
    {
      glVertex3d(trajectory[i - step].x, trajectory[i - step].y, trajectory[i - 1].z);
      glVertex3d(trajectory[i].x, trajectory[i].y, trajectory[i].z);
    }
    glEnd();
    glFlush();
  }
}
